package com.xxy.book.dto;

import lombok.Builder;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Xu XianYun
 * @Date 2023-01-06
 * @Description
 */
@Data
@Builder
public class UserDetailDTO implements UserDetails {

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码(数据库获取的加盐加密密码)
     */
    private String password;

    /**
     * 模拟从持久层查询得到的角色列表
     */
    private final List<String> roleList = new ArrayList<>();

    public UserDetailDTO() {
        this.roleList.add("USER_ROLE");
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.roleList.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toSet());
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
